import moment from "moment";

/**
 * @description: 格式化时间
 * @param {number} value
 * @param {*} format
 * @return {*}
 */
function formatDate(value: number, format = "YYYY-MM-DD HH:mm:ss") {
  if (value && typeof value === "number" && String(value).length === 10) {
    value = Number(value + "000");
  }
  const val = value ? moment(Number(value)).format(format) : "";
  return val;
}

export const filters = {
  formatDate: formatDate,
  formatDay: (value: number) => {
    return formatDate(value, "YYYY-MM-DD");
  },
  formatWeek: (value: number) => {
    const weekTextObj = {
      1: "周一",
      2: "周二",
      3: "周三",
      4: "周四",
      5: "周五",
      6: "周六",
      7: "周日"
    };
    if (value && typeof value === "number" && String(value).length === 10) {
      value = Number(value + "000");
    }
    const val: keyof typeof weekTextObj = Number(moment(Number(value)).format("E")) as keyof typeof weekTextObj;
    return val ? weekTextObj[val] : "-";
  }
};
